/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihomeclient;

import be.contribute.common.pojo.Temperature;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author debian
 */
public class ServiceClient {
    
    String URL;
    WebResource service;

    public ServiceClient(String pURL) {
        this.URL = pURL;
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        //service = client.resource("http://192.168.1.128:8080/PiHome");
        service = client.resource("http://"+URL);
    }
    
    public void insertTemperature(Temperature pTemperature)
    {
        try
        {
            service.path("tempInput")
                   .type(MediaType.APPLICATION_JSON)
                   .post(Temperature.class, pTemperature);
        }
        catch(UniformInterfaceException ex)
        {
            if(ex.getMessage().indexOf("404") > 0)
                System.out.println("Couldn't connect to web service. Status code 404");
        }
    }
    
    public void controlAirco(String pRoomId, String pState)
    {
        try
        {
            service.path("airco")
                   .queryParam("id", pRoomId)
                   .queryParam("state", pState)
                   .get(String.class);        
        }
        catch(UniformInterfaceException ex)
        {
            if(ex.getMessage().indexOf("404") > 0)
                System.out.println("Couldn't connect to web service. Status code 404");
        }
    }
    
    public void controlHeat(String pRoomId, String pState)
    {
        try
        {
            service.path("heater")
                   .queryParam("id", pRoomId)
                   .queryParam("state", pState)
                   .get(String.class);                  
        }
        catch(UniformInterfaceException ex)
        {
            if(ex.getMessage().indexOf("404") > 0)
                System.out.println("Couldn't connect to web service. Status code 404");
        }
    }
}
